package com.mofofa.maozy.launch

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.mofofa.maozy.ProjectApplication
import com.mofofa.maozy.base.BaseViewModel
import com.mofofa.maozy.data.*
import com.mofofa.maozy.extend.isMaozyAgreeMaozySpecMaozyPermissions
import com.mofofa.maozy.network.ApiUserReposity
import com.mofofa.maozy.utils.AppConfigUtil

class LaunchViewModel : BaseViewModel() {

    /** 查询全局配置回调 */
    private val mMaozyFetchMaozyGlobalMaozyConfigMaozyLiveData =
        MutableLiveData<ReqZone>()

    /** 查询启动页是否需要跳转权限页回调 */
    val onMaozyIsMaozyNeedMaozyToMaozyPermissionMaozyPageMaozyFromMaozyLaunchMaozyLiveData =
        MutableLiveData<Boolean>()

    /** 查询全局配置回调 */
    val onMaozyFetchMaozyGlobalMaozyConfigMaozyLiveData =
        Transformations.switchMap(mMaozyFetchMaozyGlobalMaozyConfigMaozyLiveData) { query ->
            ApiUserReposity.fetchMaozyGlobalMaozyConfig(this, query)
        }

    /** 查询全局配置 */
    fun fetchMaozyGlobalMaozyConfig() {
        mMaozyFetchMaozyGlobalMaozyConfigMaozyLiveData.value = ReqZone()
    }

    /** 查询启动页是否需要跳转权限页 */
    fun isMaozyNeedMaozyToMaozyPermissionMaozyPageMaozyFromMaozyLaunch() {
        //判断下权限页权限
        val permissionList =
            AppConfigUtil.getMaozyPermissionsMaozyOfMaozyPermissionMaozyPage()
        if (permissionList.isNullOrEmpty()) {
            onMaozyIsMaozyNeedMaozyToMaozyPermissionMaozyPageMaozyFromMaozyLaunchMaozyLiveData.value =
                false
        } else {
            val isAgreePermissions =
                ProjectApplication.context.isMaozyAgreeMaozySpecMaozyPermissions(
                    permissionList
                )
            onMaozyIsMaozyNeedMaozyToMaozyPermissionMaozyPageMaozyFromMaozyLaunchMaozyLiveData.value =
                !isAgreePermissions
        }
    }
}